package com.vcaresolution.logistic.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vcaresolution.logistic.form.ContactaddressForm;
import com.vcaresolution.logistic.pojo.City;
import com.vcaresolution.logistic.pojo.Contact;
import com.vcaresolution.logistic.pojo.Contactaddress;
import com.vcaresolution.logistic.pojo.Country;
import com.vcaresolution.logistic.pojo.State;
import com.vcaresolution.logistic.to.ContactaddressTO;
import com.vcaresolution.util.Utils;

@Service
public class ContactaddressServiceImpl extends BaseService implements ContactaddressService{

	private Contactaddress populateContactaddressPojo(ContactaddressForm contactaddressForm){
		Contactaddress contactaddress = new Contactaddress();
		BeanUtils.copyProperties(contactaddressForm, contactaddress);
		
		return contactaddress;
	}
	
	@Override
	@Transactional
	public void add(ContactaddressForm contactaddressForm) {
		Contactaddress contactaddress = populateContactaddressPojo(contactaddressForm);
		
		contactaddress.setCountry(getDaoFactory().getUtilityDAO().findById(Country.class, contactaddressForm.getCountryid()));
		contactaddress.setState(getDaoFactory().getUtilityDAO().findById(State.class, contactaddressForm.getStateid()));
		contactaddress.setCity(getDaoFactory().getUtilityDAO().findById(City.class, contactaddressForm.getCityid()));
		contactaddress.setContact(getDaoFactory().getUtilityDAO().findById(Contact.class, contactaddressForm.getContactid()));
		
		contactaddress.setCreationdate(Utils.getCurrentDate());
				
		getDaoFactory().getUtilityDAO().add(contactaddress);
	}

	@Override
	@Transactional
	public void update(ContactaddressForm contactaddressForm) {
		Contactaddress contactaddress = populateContactaddressPojo(contactaddressForm);
		
		contactaddress.setCountry(getDaoFactory().getUtilityDAO().findById(Country.class, contactaddressForm.getCountryid()));
		contactaddress.setState(getDaoFactory().getUtilityDAO().findById(State.class, contactaddressForm.getStateid()));
		contactaddress.setCity(getDaoFactory().getUtilityDAO().findById(City.class, contactaddressForm.getCityid()));
		contactaddress.setContact(getDaoFactory().getUtilityDAO().findById(Contact.class, contactaddressForm.getContactid()));
		
		getDaoFactory().getUtilityDAO().update(contactaddress);
	}

	@Override
	@Transactional
	public void delete(ContactaddressForm contactaddressForm) {
		getDaoFactory().getUtilityDAO().delete(populateContactaddressPojo(contactaddressForm));
	}

	@Override
	@Transactional
	public ContactaddressTO findById(Serializable contactaddressID) {
		Contactaddress contactaddress = (Contactaddress) getDaoFactory().getUtilityDAO().findById(Contactaddress.class,contactaddressID);
		
		ContactaddressTO contactaddressTO = new ContactaddressTO();
		BeanUtils.copyProperties(contactaddress, contactaddressTO);
		
		contactaddressTO.setCityname(contactaddress.getCity().getCityname());
		
		return contactaddressTO; 
	}

	@Override
	@Transactional
	public List<ContactaddressTO> findByAll(Serializable contactId) {
		List<Contactaddress> contactaddressesList = getDaoFactory().getUtilityDAO().findByCondition(Contactaddress.class, Utils.getEqExpressionList("contact.contactid", contactId));
		
		List<ContactaddressTO> contactaddressFormList = new ArrayList<ContactaddressTO>();
		
		if( contactaddressesList != null && !contactaddressesList.isEmpty() ){
			ContactaddressTO contactaddressForm = null;
			for(Contactaddress contactaddress: contactaddressesList){
				contactaddressForm = new ContactaddressTO();
				
				BeanUtils.copyProperties(contactaddress, contactaddressForm);
				
				contactaddressForm.setCityname(contactaddress.getCity().getCityname());
				
				contactaddressFormList.add(contactaddressForm);
			}
		}		
		return contactaddressFormList;
	}
}